package com.example.lianxi222.fragment.home;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.lianxi222.R;
import com.example.lianxi222.adapter.RecyclerAdapter;
import com.example.lianxi222.databinding.FragmentFirstBinding;
import com.example.mvvm_base.userbean.BannerEntity;
import com.example.mvvm_base.userbean.LikeEntity;
import com.example.mvvm_base.userbean.VideoEntity;
import com.example.mvvm_base.view.BaseFragment;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;


public class FirstFragment extends BaseFragment<FragmentFirstBinding,HomeViewModel> {
  private List<BannerEntity.DataBean> bannerList=new ArrayList<>();
  private List<String> bannerPath=new ArrayList<>();
  private List<VideoEntity.DataBean> videoList=new ArrayList<>();
  private RecyclerAdapter recyclerAdapter;
  private int currentPosition=0;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_first;
    }

    @Override
    protected void initData() {
        //清除集合
        bannerList.clear();
        videoList.clear();
        //获取数据
        viewModel.getBannerData();
        viewModel.getVideoData();
        //banner观察
     viewModel.bannerSuccess.observe(this, bannerEntity -> {
         if(bannerEntity.getCode()==200){
             bannerList.addAll(bannerEntity.getData());
             Log.d("sss", "banner: "+bannerEntity.getData().size());
             for (int i = 0; i < bannerList.size(); i++) {
                 bannerPath.add(bannerList.get(i).getImagePath());
             }
             dataBinding.banner.setImages(bannerPath);
             dataBinding.banner.start();
         }
     });
     viewModel.bannerFailure.observe(this, s -> ToastUtils.showLong(s));
     //video观察
     viewModel.videoSuccess.observe(this, videoEntity -> {
         if(videoEntity.getCode()==200){
             videoList.addAll(videoEntity.getData());
             recyclerAdapter.notifyDataSetChanged();
             Log.d("sss", "video: "+videoEntity.getData().size());
         }else{
             ToastUtils.showLong(videoEntity.getMessage());
         }
     });
     viewModel.videoFailure.observe(this, s -> ToastUtils.showLong(s));
     viewModel.likeSuccess.observe(this, likeEntity -> {
         if(likeEntity.getCode()==200){
             ToastUtils.showLong(likeEntity.getMessage());
             videoList.get(currentPosition).setDianzan(1);
             recyclerAdapter.notifyItemChanged(currentPosition);
         }else{
             ToastUtils.showLong(likeEntity.getMessage());
         }
     });
     viewModel.likeFailure.observe(this, s -> ToastUtils.showLong(s));
        viewModel.unLikeSuccess.observe(this, likeEntity -> {
            if(likeEntity.getCode()==200){
                ToastUtils.showLong(likeEntity.getMessage());
                videoList.get(currentPosition).setDianzan(0);
                recyclerAdapter.notifyItemChanged(currentPosition);
            }else{
                ToastUtils.showLong(likeEntity.getMessage());
            }
        });
        viewModel.unLikeFailure.observe(this, s -> ToastUtils.showLong(s));
        //和


    }

    @Override
    protected void initView() {
        dataBinding.rec.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        recyclerAdapter=new RecyclerAdapter(R.layout.item1,videoList);
        dataBinding.rec.setAdapter(recyclerAdapter);
        dataBinding.banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path.toString()).into(imageView);
            }
        });
        recyclerAdapter.addChildClickViewIds(R.id.first_like);
        recyclerAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            currentPosition=position;
            if(view.getId()==R.id.first_like){
                VideoEntity.DataBean dataBean = videoList.get(position);
                if(dataBean.getDianzan()==0){
                    viewModel.like(dataBean.getId());
                }else{
                    viewModel.unLike(dataBean.getId());
                }
            }
        });
    }

    @Override
    protected Class<HomeViewModel> getViewModel() {
        return HomeViewModel.class;
    }


}